﻿; (function ($) {
    var methods = {
        init: function (value) {
            var $this = $(this);
            if (value) {
                $this.val(value);
            } else {
                $this.each(function () {
                    var val = $(this).attr('selectvalue');
                    if (val != undefined) {
                        $(this).val(val);
                    }
                });
            }
            if ($(window).width() < 768) {
                $this.mobiscroll().select({
                    theme: "bootstrap", lang: "zh", display: "bottom", mode: "scroller", rows: 5, defaultValue: "3", buttons: [
                        {
                            text: window.Lang['text_wmp_confirm'],//'确认'
                            handler: 'set',
                            cssClass: 'dwb btn btn-primary'
                        },
                        {
                            text: window.Lang['text_wmp_clear'],//'清除',
                            handler: 'clear',
                            cssClass: 'dwb btn btn-default'
                        }
                    ],
                    onBeforeShow: function (inst) {
                        if ($(this).attr("disabled")) {
                            return false;
                        }
                    }
                });
            }
        },
        setData: function (data) {
            var $this = $(this);
            var url = ""; //数据源的url
            var param = {}; //查询数据的参数
            var isNeedEmpty = true; //是否需要空行
            var singleSelected = false;//如果只有一条数据的时候是否默认选中，不管有没有空行
            var dataSource = "";
            if (data != undefined && data.length > 0) {
                url = data[0].url;
                param = data[0].param;
                if (data[0].isNeedEmpty != undefined) {
                    isNeedEmpty = data[0].isNeedEmpty;
                }
                if (data[0].singleSelected != undefined) {
                    singleSelected = data[0].singleSelected;
                }
                if (data[0].dataSource != undefined) {
                    dataSource = data[0].dataSource;
                }
            }
            if (url != "" && url != undefined) {
                AJAXAPI(url, param, function (d) {
                    var html = "";
                    if ($(d).length > 0) {
                        if ($(d).length == 1) {
                            if (singleSelected) {
                                $(d).each(function (index, item) {
                                    html += '<option value="' + item.Value + '">' + item.Name + '</option>';
                                });
                            }
                            else {
                                if (isNeedEmpty)
                                    html += '<option value=""></option>';
                                $(d).each(function (index, item) {
                                    html += '<option value="' + item.Value + '">' + item.Name + '</option>';
                                });
                            }
                        }
                        else {
                            if (isNeedEmpty)
                                html += '<option value=""></option>';
                            $(d).each(function (index, item) {
                                html += '<option value="' + item.Value + '">' + item.Name + '</option>';
                            });
                        }
                    } else {
                        html += '<option value=""></option>'; //没有数据的话不管是否需要空行都加上空行
                    }
                    $this.html(html);
                    var val = $this.attr('selectvalue');
                    if (val != undefined) {
                        $this.val(val);
                    }
                    $this.WmpSelectpicker("init");
                    if ($this.attr("endLoad") != "" && $this.attr("endLoad") != undefined) {
                        var SelectorConfirm = null;
                        eval("SelectorConfirm=" + $this.attr("endLoad"));
                        if ($.isFunction(SelectorConfirm)) {
                            SelectorConfirm($this);
                        }
                    }
                }, undefined, false);
            }
            else {
                var html = "";
                if ($(dataSource).length > 0) {
                    if ($(dataSource).length == 1) {
                        if (singleSelected) {
                            $(dataSource).each(function (index, item) {
                                html += '<option value="' + item.Value + '">' + item.Name + '</option>';
                            });
                        }
                        else {
                            if (isNeedEmpty)
                                html += '<option value=""></option>';
                            $(dataSource).each(function (index, item) {
                                html += '<option value="' + item.Value + '">' + item.Name + '</option>';
                            });
                        }
                    }
                    else {
                        if (isNeedEmpty)
                            html += '<option value=""></option>';
                        $(dataSource).each(function (index, item) {
                            html += '<option value="' + item.Value + '">' + item.Name + '</option>';
                        });
                    }
                } else {
                    html += '<option value=""></option>'; //没有数据的话不管是否需要空行都加上空行
                }
                $this.html(html);
                var val = $this.attr('selectvalue');
                if (val != undefined) {
                    $this.val(val);
                }
                $this.WmpSelectpicker("init");
                if ($this.attr("endLoad") != "" && $this.attr("endLoad") != undefined) {
                    var SelectorConfirm = null;
                    eval("SelectorConfirm=" + $this.attr("endLoad"));
                    if ($.isFunction(SelectorConfirm)) {
                        SelectorConfirm($this);
                    }
                }
            }
        },
        enable: function () {
            $(this).parent(".selectcontrol").find("input,select").attr("disabled", false);
        },
        disabled: function () {
            $(this).parent(".selectcontrol").find("input,select").attr("disabled", true);
        },
        getValue: function () {
            return $(this).val();
        },
        getText: function () {
            return $(this).find("option:selected").text();
        }
    };
    $.fn.WmpSelectpicker = function (method) {
        if (window != window.parent && typeof (window.parent.$("#").WmpSelectpicker) == "function") {
            if (arguments.length > 1)
                return window.parent.$(this).WmpSelectpicker(method, arguments[1]);
            else {
                return window.parent.$(this).WmpSelectpicker(method);
            }
        } else {
            if (methods[method]) {
                return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
            } else if (typeof method === 'object' || !method) {
                return methods.init.apply(this, arguments);
            } else {
                $.error('Method' + method + 'does not exist');
            }
        }
    };
})($);


; (function ($, window, undefined) {
    "use strict"
    var mSelect, _ulitity, Variable = {}, $this, _variable,SelectedArray,RemoteData = {};
    _ulitity = {
        mobile: function () {
            return !!navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i);
        },
        iframe: function () {
            return window.self === window.top;
        },
        width: function () {
            return _ulitity.mobile() ? (!_ulitity.iframe() ? top.window.screen.width : $(window).width()) : _const.width;
        },
        container: function () {
            if (_ulitity.mobile()) {
                return _ulitity.mContainer();
            }

            var _p = _ulitity.position();
            var _html = "<div class=\"select-options-item\" style='left:" + _p.left + "px;top:" + _p.top + "px;width:" + _p.width + "px;height:" + _p.height + "px;" + (_p.bTop ? "border-top:1px solid #ccc;" : "") + "'>";
            if (_variable.options.search && _variable.options.multy) {
                _html += "<div class='mselect-search'>";
                _html += "  <input type='text' class='mselect-search-input' />";
                _html += "  <i class='fa fa-search mselect-search-icon'/>";
                _html += "</div>";
            }
            _html += "  <ul>";
            _html += _ulitity.render();
            _html += "  </ul>";
            _html += "</div>";

            return _html;
        },
        mContainer: function () {
            var maskHeight = $(window).scrollTop() + $(window).height(),
                _length = _ulitity.source().length,
                _containerHeight = 0,
                _contentHeight = 0;

            var _title = $this.parents(".wmp-select").attr("title");
            if (_title == "undefined" || !_title) {
                if (_variable.options.title) {
                    _title = _variable.options.title;
                } else {
                    _title = "";
                }
            }

            var _hasTitle = _title ? 1 : 0;
            if (_length > 5) {
                _containerHeight = (5 + _hasTitle) * 40;
                _contentHeight = (5) * 40;
            } else {
                _containerHeight = (_length + _hasTitle) * 40;
                _contentHeight = _length * 40;
            }

            var _mHtml = "<div class='mselect-mask' style='height:" + maskHeight + "px'></div>";
            _mHtml += "<div class='mselect-mobile' style='height:" + _containerHeight + "px'>";
            _mHtml += _title ? "     <div class='mselect-mobile-title'>" + _title + "</div>" : "";
            _mHtml += "     <div class='mselect-mobile-content' style='height:" + _contentHeight + "px'>";
            _mHtml += "         <ul>";
            _mHtml += _ulitity.render();
            _mHtml += "         </ul>";
            _mHtml += "     </div>";
            _mHtml += "</div>";

            _ulitity.event.mask();

            return _mHtml;
        },
        position: function () {
            var _pC = $this.parents(".wmp-select").find(".mselect-left");
            var _offset = _pC.offset(),
                _top = _offset.top + _pC.outerHeight(true) + 1,
                _left = $this.parents(".wmp-select").offset().left,
                scrollTop = $(window).scrollTop(),
                _length = _ulitity.source().length,
                contentHeight = (_length + ((_variable.options.search && _variable.options.multy) ? 1 : 0)) * 30 + 2,
                _bTop = false;

            if (_variable.options.max > 0) {
                if (_length > _variable.options.max) {
                    contentHeight = (_variable.options.max + ((_variable.options.search && _variable.options.multy) ? 1 : 0)) * 30;
                }
            }

            if (_top + contentHeight > (scrollTop + $(window).height())) {
                _top = _top - contentHeight - $this.outerHeight(true) - 2;
                _bTop = true;
            }
            return {
                top: _top,
                left: _left,
                width: $this.parents(".wmp-select").outerWidth(),
                bTop: _bTop,
                height: contentHeight
            };
        },
        close: {
            initiative: function () {
                var $body = $this.parents("body");
                $body.removeClass("mselect-body");
                if ($body.find("[mselect-active='1']").length == 0) { return; }

                var val = _ulitity.get($("[mselect-active='1']"));
                if (val && val.length == 0) {
                    val = undefined;
                }
                if (val) {
                    val = _variable.options.multy ? val : val[0]
                }
                _variable.exec('close', $this, val || "");

                SelectedArray = null;
                $body.find("[mselect-active='1']").parents(".wmp-select").find("i").removeClass("fa-caret-up").addClass("fa-caret-down")
                $body.find("[mselect-active='1']").removeAttr("mselect-active").removeClass("active");
                if (_ulitity.mobile()) {
                    $body.find(".mselect-mask,.mselect-mobile").remove();
                } else {
                    $body.find(".select-options-item").remove();
                }
            },
            passive: function (e) {
                if (e.clientX == 0 && e.clientY == 0) {
                    return;/*被动触发click事件，点击触发*/
                }
                var _$ele = $(e.srcElement).parents("body").find(".select-options-item");
                if (_$ele.length == 0) {
                    _$ele = $(".select-options-item");
                    _$ele.hide();
                }
                if (_$ele.is(":visible")) {
                    var left_min = _$ele.offset().left,
                        left_max = left_min + _$ele.outerWidth(),
                        top_min = _$ele.offset().top - _$ele.parents(".wmp-select").outerHeight(),
                        top_max = _$ele.offset().top + _$ele.outerHeight(),
                        scrollTop = $(window).scrollTop();
                    if (e.clientX >= left_min && e.clientX <= left_max && (e.clientY + scrollTop) >= top_min && (e.clientY + scrollTop) <= top_max) { } else {
                        _ulitity.close.initiative();
                    }
                }
            }
        },
        exist: function (value) {
            var old = _ulitity.get(),
                has = true;
            if (old && old.length > 0) {
                $(old).each(function () {
                    if (this.value == value) {
                        has = false;
                    }
                });
            }
            return has;
        },
        event: {
            li_option: function () {
                var $body = $this.parents("body");
                $body.find(".select-options-item").off("click", "li");
                $body.find(".select-options-item").on("click", "li", function (e) {
                    e.preventDefault();
                    var _this = $(this);
                    var _val = _this.attr("val"),
                        _text = _this.text();
                    if (!_variable.options.multy) {
                        if (!_val) {
                            _text = "";
                        }
                        $this.val(_text);

                        if (_ulitity.exist(_val)) {
                            SelectedArray = [];
                            SelectedArray[0] = { "text": _text, "value": _val };
                            _ulitity.set();
                            _variable.exec('change', $this, SelectedArray[0]);
                        }
                        _ulitity.close.initiative();
                        return;
                    } else {
                        if (!_val) { return; }
                        SelectedArray = [];
                        _this.toggleClass("active");
                        _val = "";
                        $(".select-options-item li.active").each(function () {
                            _val += $(this).text() + ",";
                            SelectedArray.push({ "text": $(this).text(), "value": $(this).attr("val") });
                        });
                        if (_val.length > 0) {
                            _val = _val.substring(0, _val.length - 1);
                        }
                        $this.val(_val);

                        _variable.exec('change', $this, SelectedArray);
                    }
                    _ulitity.set();
                    return;
                });
            },
            search: function () {
                var $body = $this.parents("body");
                $body.find(".select-options-item").off("input propertychange", "input.mselect-search-input");
                $body.find(".select-options-item").on("input propertychange", "input.mselect-search-input", function (e) {
                    e.preventDefault();
                    var _html = _ulitity.render($(this).val().trim());
                    $body.find(".select-options-item ul").html(_html);
                });
            },
            trigger: function (key) {
                if (!_variable.options.write && $this.attr("mselect-active")) {
                    _ulitity.close.initiative();
                    return;
                }
                var $body=$this.parents("body");
                $body.find(".select-options-item").length > 0 ? _ulitity.close.initiative() : "";

                $body.find("[mselect-active='1']").removeAttr("mselect-active");
                $this.attr("mselect-active", "1").addClass("active");
                if (_variable.options.write || (!_variable.options.multy && _variable.options.search)) {
                    $this.removeAttr("readonly");
                } else {
                    $this.attr("readonly", true);
                    document.activeElement.blur();
                }

                SelectedArray = _ulitity.get();
                $body.append(_ulitity.container());
                $body.find("[mselect-active='1']").parents(".wmp-select").find("i").removeClass("fa-caret-down").addClass("fa-caret-up");

                if (!_ulitity.mobile()) {
                    _ulitity.event.li_option();
                    if (_variable.options.search) {
                        _ulitity.event.search();
                    }
                } else {
                    _ulitity.event.mobile_li();
                }
            },
            mask: function () {
                var $body = $this.parents("body");
                $body.addClass("mselect-body");
                $body.off("touchstart", ".mselect-mask");
                $body.on("touchstart", ".mselect-mask", function (e) {
                    e.preventDefault();
                    _ulitity.close.initiative();
                    return;
                });
            },
            mobile_li: function () {
                var $body = $this.parents("body");
                $body.find(".mselect-mobile").off("click", "li");
                $body.find(".mselect-mobile").on("click", "li", function (e) {
                    e.preventDefault();
                    var _this = $(this);
                    var _val = _this.attr("val"),
                        _text = _this.text();
                    if (!_variable.options.multy) {
                        /*单选*/
                        if (!_val) {
                            _text = "";
                        }
                        $this.val(_text);
                        if (_ulitity.exist(_val)) {
                            SelectedArray = [];
                            SelectedArray[0] = { "text": _text, "value": _val };
                            _ulitity.set();
                            _variable.exec('change', $this, SelectedArray[0]);
                        }
                        _ulitity.close.initiative();
                        return;
                    } else {
                        /*多选*/
                        if (!_val) { return; }
                        SelectedArray = [];
                        _this.toggleClass("active");
                        _val = "";
                        $(".mselect-mobile-content li.active").each(function () {
                            _val += $(this).text() + ",";
                            SelectedArray.push({ "text": $(this).text(), "value": $(this).attr("val") });
                        });
                        if (_val.length > 0) {
                            _val = _val.substring(0, _val.length - 1);
                        }
                        $this.val(_val);
                        _variable.exec('change', $this, SelectedArray);
                        _ulitity.set();
                    }
                });
            }
        },
        get: function ($obj) {
            var _a = $obj || $this;
            if (_a.length > 0)
                return $.hasData(_a[0]) ? $.data(_a[0], 'selected') : [];
            else {
                return [];
            }
        },
        set: function () {
            $.data($this[0], "selected", SelectedArray);
        },
        selected: function (value) {
            if (!SelectedArray || SelectedArray.length == 0) {
                return;
            }
            var has = false;
            $(SelectedArray).each(function () {
                if (value && this.value == value) {
                    has = true;
                    return false;
                }
            });

            return has;
        },
        render: function (key) {
            var _html = "", _data = _ulitity.source(), hasRegex = false;
            $(_data).each(function () {
                var _text = this.text,
                    _val = this.value,
                    checked = _ulitity.selected(_val);
                if (_ulitity.mobile()) {
                    _html += "<li " + (checked ? "class='active'" : "") + "  val='" + _val + "'><i class='fa fa-check mselect-mobile-icon'/><div class='vh-wrapper'><div class='vh-cell'><p class='vh-p'>" + _text + "</p></div></div></li>";
                } else {
                    if (!key) {
                        _html += "<li " + (checked ? "class='active'" : "") + "  val='" + _val + "'><div class='vh-wrapper'><div class='vh-cell'><p class='vh-p'>" + _text + "</p></div></div><i class='fa fa-check'/></li>";
                    } else {
                        if (this.text.toLowerCase().indexOf(key.toLowerCase()) > -1) {
                            hasRegex = true;
                            _html += "<li " + (checked ? "class='active'" : "") + "  val='" + _val + "'><div class='vh-wrapper'><div class='vh-cell'><p class='vh-p'>" + _text + "</p></div></div><i class='fa fa-check'/></li>";
                        } else {
                            _html += "<li " + (checked ? "class='active'" : "") + "  val='" + _val + "' style='display:none;'><div class='vh-wrapper'><div class='vh-cell'><p class='vh-p'>" + _text + "</p></div></div><i class='fa fa-check'/></li>";
                        }
                    }
                }
            });
            if (key && !hasRegex) {
                _html = "<li>No matching data</li>";
            }
            return _html;
        },
        source: function (options) {
            if ((options && options.remote) || (_variable && _variable.options.remote)) {
                if (!RemoteData[_variable.options.key]) {
                    sourceArray = JSON.parse(_ulitity.remote(_variable.options, true));
                } else {
                    sourceArray = JSON.parse(RemoteData[_variable.options.key]);
                }
            } else if ((options && options.data) || (_variable && _variable.options.data)) {
                sourceArray = _variable.options.data;
            } else {
                var _sourceObj = $this.parents(".wmp-select").find("select option"), sourceArray = [];
                _sourceObj.each(function () {
                    var _text = $(this).text(),
                        _value = $(this).attr("value");

                    if (!_text) { return; }
                    if (!_value) {
                        if (_value == undefined) {
                            /*option 没有设置value属性，则将text同步为value*/
                            _value = _text;
                        } else {
                            _value = "";
                        }
                    }
                    sourceArray.push({ "text": _text, "value": _value });
                });
            }

            if (!sourceArray || sourceArray.length == 0) {
                sourceArray.push({ text: "no data", value: "" });
            }

            return sourceArray;
        },
        remote: function (options, async) {
            var url = options.remote.url,
                param = options.remote.param,
                type = options.remote.type;

            if (async) {
                RemoteData[options.key] = "";
                AJAX(url, param, function (data) {
                    RemoteData[options.key] = data;
                }, type, true, false);
                return RemoteData[options.key];
            }

            AJAX(url, param, function (data) {
                RemoteData[options.key] = data;
            }, type);
        },
        inputObject: function ($dom) {
            return _ulitity.oType($dom) ? $dom.parents(".wmp-select").find("input.mselect-left-input") : $dom.find("input.mselect-left-input");
        },
        oType: function ($object) {
            return ($object[0].nodeName.toLowerCase() == "select");
        }
    };

    if (document.removeEventListener) {
        document.removeEventListener("click", _ulitity.close.passive);
    } else if (document.detachEvent) { /*IE 8*/
        document.detachEvent("click", _ulitity.close.passive);
    }
    document.addEventListener("click", _ulitity.close.passive, false);

    mSelect = function ($dom, options) {
        return this.init($dom, options);
    };

    mSelect.prototype = {
        init: function ($dom, options) {
            options.trigger = (_ulitity.mobile() ? "touchstart" : "click");
            this["options"] = options,
            this["event-array"] = { change: null, close: null };
            var $body=$dom.parents("body");
            if ($body.find("input[mselect-key='" + options.key + "']").length > 0) { return this; }

            $dom.hide();
            $dom.each(function () {
                if ($(this).attr("mselect-key") != undefined || $(this).find("[mselect-key]").length > 0) {
                    return true;
                }

                var _this = $(this),
                    _placeholder = _this.attr("placeholder"),
                    _selectHtml = "<div class='mselect-right icon-" + options.key + "'>";
                $this = _this;
                if (!_placeholder && options.title) {
                    _placeholder = options.title;
                }

                if (!_placeholder) {
                    if (options.title) {
                        _placeholder = options.title;
                    } else {
                        _placeholder = "";
                    }
                }

                _selectHtml += "     <div class='mselect-right-icon'><i class='fa fa-caret-down'></i></div>";
                _selectHtml += "   </div>";
                _selectHtml += "   <div class='mselect-left'>";
                _selectHtml += "     <input type='text' class='mselect-left-input' mselect-key='" + options.key + "' placeholder='" + _placeholder + "'/>";
                _selectHtml += "   </div>";

                var inputObject;
                if (!_ulitity.oType(_this)) {
                    _this.html(_selectHtml).show();
                    inputObject = _this.find("input.mselect-left-input");
                } else {
                    var _container = _this.parents(".wmp-select");
                    _container.find("i").remove();
                    _this.after(_selectHtml);
                    inputObject = _container.find("input.mselect-left-input");
                }

                var defaulValue = _this.attr("value"),/*设置默认值*/
                      text = "",
                      defaultArray = [];
                if (defaulValue) {
                    if (defaulValue.indexOf('[') > -1) {
                        var tempVal = JSON.parse(defaulValue);
                        defaulValue = "";
                        $(tempVal).each(function () {
                            defaulValue += this.text + ",";
                        });
                        if (defaulValue) {
                            defaulValue = defaulValue.substring(0, defaulValue.length - 1);
                        }
                    }
                    var setArray = defaulValue.split(',');
                    if (setArray && setArray.length > 0) {
                        var source = _ulitity.source(options);
                        for (var s1 = 0, s2 = setArray.length; s1 < s2; s1++) {
                            for (var a1 = 0, a2 = source.length; a1 < a2; a1++) {
                                if (setArray[s1] == source[a1].value) {
                                    text += source[a1].text + ",";
                                    defaultArray.push(source[a1]);
                                }
                            }
                        }

                        if (text && text.length > 0) {
                            text = text.substring(0, text.length - 1);
                        }

                        if (!text) {
                            text = defaulValue;
                            defaultArray.push({ text: defaulValue, value: defaulValue });
                        }

                        inputObject.val(text);
                        $.data(inputObject[0], "selected", defaultArray);
                    }
                }

                if (_this.attr("select-multy")) {
                    inputObject.attr("select-multy", _this.attr("select-multy"));
                }
                if (_this.attr("select-search")) {
                    inputObject.attr("select-search", _this.attr("select-search"));
                }
                if (_this.attr("select-write")) {
                    inputObject.attr("select-write", _this.attr("select-write"));
                }
            });
            $dom.parent().find("input[mselect-key='" + options.key + "']").on(options.trigger, function (e) {
                e.stopPropagation();
                $this = $(this);
                _variable = Variable[options.key];
                if (!_variable.options.enable) {
                    return;
                }
                if (_ulitity.mobile() && _variable.options.write) {
                    $(".mselect-mobile").length > 0 ? _ulitity.close.initiative() : "";
                    return;
                }

                _variable.compatible();

                _ulitity.event.trigger(options.key);
                return false;
            }).on("input propertychange", function (e) {
                $this = $(this);
                e.preventDefault();
                if (!_variable) { return; }

                var inputVal = $(this).val().trim();
                var old = _ulitity.get();

                if (!inputVal && (!old || (old && !old[0].value && !old[0].text))) {
                    $body.find('.select-options-item').remove();
                    $body.append(_ulitity.container());
                    if (!_ulitity.mobile()) {
                        _ulitity.event.li_option();
                        if (_variable.options.search) {
                            _ulitity.event.search();
                        }
                    } else {
                        _ulitity.event.mobile_li();
                    }
                    return false;
                }

                if (_variable.options.write) {
                    _variable.exec('change', $this, { "text": inputVal, "value": inputVal });
                    SelectedArray = [];
                    SelectedArray[0] = { "text": inputVal, "value": inputVal };
                    _ulitity.set();
                }

                if (_variable.options.search && !_variable.options.multy) {
                    if ($body.find('.select-options-item').length == 0) {
                        $body.append(_ulitity.container());
                    }
                    var o_top = $body.find(".select-options-item").offset().top;
                    var _html = _ulitity.render($(this).val().trim());
                    $body.find(".select-options-item ul").html(_html);

                    var _pC = $this.parents(".wmp-select").find(".mselect-left");
                    var _offset = _pC.offset(),
                          _top = _offset.top + _pC.outerHeight(true) + 1,
                          _left = $this.parents(".wmp-select").offset().left,
                          scrollTop = $(window).scrollTop(),
                          _length = $body.find(".select-options-item ul li:visible").length,
                          contentHeight = (_length + ((_variable.options.search && _variable.options.multy) ? 1 : 0)) * 30 + 2,
                          _bTop = false;

                    if (_variable.options.max > 0) {
                        if (_length > _variable.options.max) {
                            contentHeight = (_variable.options.max + ((_variable.options.search && _variable.options.multy) ? 1 : 0)) * 30;
                        }
                    }

                    if (o_top < _offset.top) {
                        /*上方区域*/
                        _top = _top - contentHeight - _pC.outerHeight(true);
                        _bTop = true;
                    }
                    $body.find(".select-options-item").css({
                        "left": _left + "px",
                        "top": _top + "px",
                        "height": contentHeight + "px"
                    });
                }
            });

            $dom.parent().find("div.icon-" + options.key).on(options.trigger, function (e) {
                e.preventDefault();
                _variable = Variable[options.key];
                if (!_variable.options.enable) {
                    return;
                }
                $this = $(this).next().find("input.mselect-left-input");
                document.activeElement.blur();
                _variable.compatible();

                _ulitity.event.trigger(options.key);
                if (!_ulitity.mobile() && _variable.options.write) {
                    $this.focus();
                }
                return false;
            });

            $(window).off("scroll").on("scroll", function () {
                var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
                if ($body.find(".select-options-item").length > 0 && $this) {
                    $body.find(".select-options-item").css("top", ($this.offset().top + $this.outerHeight(true) - 1) + "px");
                }
            })

            if (options.remote) {
                _ulitity.remote(options);
            }

            return this;
        },
        compatible: function () {
            if (_variable) {
                if ($this.attr("select-multy")) {
                    _variable.options.multy = ($this.attr("select-multy") == "true");
                }
                if ($this.attr("select-search")) {
                    _variable.options.search = ($this.attr("select-search") == "true");
                }
                if ($this.attr("select-write")) {
                    _variable.options.write = ($this.attr("select-write") == "true");
                }
            }
        },
        on: function (event, callback) {
            this["event-array"][event] = callback;
            return this;
        },
        exec: function (event, $dom, data) {
            var _event = this["event-array"][event];
            if (_event) {
                _event.call(this, $dom, data);
            }
        }
    };

    var methods = {
        init: function (options) {
            var _this = $(this);

            if (_this.length == 0) {
                return;
            }

            var setting = {
                multy: false,
                search: false,
                write: false,
                max: 10,
                enable: true,
                data: null,
                remote: null,
                value: "",
                title: ""
            };
            setting = $.extend(setting, options);

            if (setting.value) {
                _this.attr("value", setting.value);
            }

            _this.each(function () {
                var param = {};
                function init($this) {
                    var _key = "msk-" + new Date().getTime() + "-" + Math.random().toString(36).substr(2);
                    Variable[_key] = new mSelect($($this), {
                        key: _key,
                        multy: setting.multy,
                        search: setting.search,
                        write: setting.write,
                        max: setting.max,
                        enable: setting.enable,
                        data: setting.data,
                        remote: setting.remote,
                        title: setting.title
                    });

                    if ($.isFunction(setting.change)) {
                        Variable[_key].on("change", function ($object, data) {
                            setting.change($object, data);
                        })
                    }

                    if ($.isFunction(setting.close)) {
                        Variable[_key].on("close", function ($object, data) {
                            setting.close($object, data);
                        })
                    }

                    $($this).attr("mselect-key", _key);
                }
                if ($(this).attr("datasourcetype") == "api" && window.location.href.indexOf("HtmlPage.html") < 0 && window.location.href.indexOf("PageModelEdit.html") < 0) {
                    var controlId=  $(this).attr("id").split('_')[0];
                    if (window.DataSourceList != undefined && window.DataSourceList[controlId] != undefined) {
                        setting.data = window.DataSourceList[controlId];
                        init($(this));
                    } else {
                        var $this = $(this);
                        AJAXAPI($(this).attr("AjaxUrl"), param, function (d) {
                            if (d.success||d.code==0) {
                                var data = [];
                                for (var i = 0; i < d.data.length; i++) {
                                    var item = {};
                                    item.text = d.data[i][$($this).attr("ShowValue")];
                                    item.value = d.data[i][$($this).attr("HideValue")];
                                    data.push(item)
                                }
                                if (window.DataSourceList == undefined) {
                                    window.DataSourceList = {}
                                }
                                window.DataSourceList[controlId] = data;
                                setting.data = data;
                                init($this);
                            } else { ShowError(d.msg); return false; }
                        },undefined,undefined,false);
                    }
                } else {
                    init($(this));
                }
            });


            return _this;
        },
        key: function ($this) {
            return $this.attr("mselect-key");
        },
        config: function () {
            return Variable[methods.key($(this))];
        },
        get: function () {
            var _val = _ulitity.get(_ulitity.inputObject($(this)));
            if (!_val || _val.length == 0) {
                return [];
            }
            return _val;
        },
        set: function (data) {
            if (data == undefined || data == null) {
                return;
            }
            if (typeof data == "number") {
                data = data + "";
            }
            //if (!data) { return; }
            var dataArray;
            if (data.indexOf('[') > -1 || data.indexOf('{') > -1) {
                /*赋值格式为json格式*/
                var setArray = JSON.parse(data);
                var value = "";
                for (var s = 0, e = setArray.length; s < e; s++) {
                    value += setArray[s].value + ",";
                }
                if (value && value.length > 0) {
                    value = value.substring(0, value.length - 1);
                }
                dataArray = value.split(',');
            } else {
                dataArray = data.split(',');
            }

            if (dataArray.length == 0) { return; }
            _variable = Variable[methods.key($(this))],
            $this = _ulitity.inputObject($(this)),
            SelectedArray = [];
            if (_variable!=undefined)
            _variable.compatible();

            var _source = _ulitity.source(), inputVal = "";
            for (var s = 0, e = dataArray.length; s < e; s++) {
                var _text = "";
                $(_source).each(function () {
                    if (dataArray[s] == this.value) {
                        _text = this.text;
                        return;
                    }
                });

                if (!_text) {
                    /*无匹配值*/
                    if (!_variable.options.multy) {
                        if (!_variable.options.write) {
                            return;
                        } else {
                            _text = dataArray[s];
                        }
                    } else {
                        if (!_variable.options.write) {
                            continue;
                        } else {
                            _text = dataArray[s];
                        }
                    }
                }

                inputVal += _text + ",";
                SelectedArray.push({ "text": _text, "value": dataArray[s] })
            }
            if (inputVal && inputVal.length > 0) {
                inputVal = inputVal.substring(0, inputVal.length - 1);
            }
            $this.val(inputVal);
            _ulitity.set();

            return $(this);
        },
        disabled: function () {
            if ($(this).length == 0) { return; }
            $(this).each(function () {
                var _container = $(this).parents(".wmp-select");
                _container.addClass("disabled");
                _container.find("input").attr("disabled", true);
                Variable[methods.key($(this))].options.enable = false;
            });
            return $(this);
        },
        clear: function () {
            if ($(this).length == 0) { return; }
            $(this).each(function () {
                var $obj = _ulitity.inputObject($(this));
                $obj.val("");
                $.data($obj[0], "selected", []);
            });
            return $(this);
        },
        enable: function () {
            if ($(this).length == 0) { return; }
            $(this).each(function () {
                var _container = $(this).parents(".wmp-select");
                _container.removeClass("disabled");
                _container.find("input").removeAttr("disabled");
                Variable[methods.key($(this))].options.enable = true;
            });
            return $(this);
        },
        readonly: function () {
            if ($(this).length == 0) { return; }
            var _val = _ulitity.get(_ulitity.inputObject($(this))), printVal = "";
            if (!_val || _val.length == 0) {
                printVal = "";
            } else {
                $(_val).each(function () {
                    printVal += this.text + ",";
                });
                if (printVal) {
                    printVal = printVal.substring(0, printVal.length - 1);
                }
            }

            if (_ulitity.oType($(this))) {
                $(this).parents(".wmp-select").replaceWith(printVal);
            } else {
                $(this).replaceWith(printVal);
            }
        },
        getvalue: function () {
            if ($(this).length == 0) { return; }
            var _val = $(this).WmpSelectBox("get");
            if (_val.length == 0) {
                return "";
            }
            var _temp = "";
            $(_val).each(function () {
                _temp += this.value + ",";
            });
            if (_temp) {
                _temp = _temp.substring(0, _temp.length - 1);
            }
            return _temp;
        },
        change: function (fun) {
            if ($.isFunction(fun)) {
                Variable[methods.key($(this))].on("change", function (dom, value) {
                    return fun(dom, value);
                });
            }
        },
        close: function (fun) {
            if ($.isFunction(fun)) {
                Variable[methods.key($(this))].on("close", function (dom, value) {
                    return fun(dom, value);
                });
            }
        },
        empty: function () {
            $(this).empty();
            $(this).removeAttr("mselect-key");
            $(this).parent().find(".mselect-right").remove();
            $(this).parent().find(".mselect-left").remove();
        }
    };

    $.fn.WmpSelectBox = function (method) {

            if (methods[method]) {
                return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
            } else if (typeof method === 'object' || !method) {
                return methods.init.apply(this, arguments);
            } else {
                $.error('Method' + method + 'does not exist');
            }

    };

    window.WmpSelectBox = mSelect;
}($, window));
